package com.milhouz.bomberrokon.engines;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.milhouz.bomberrokon.BomberSlashActivity;
import com.milhouz.bomberrokon.events.Event;
import com.stickycoding.Rokon.AudioStream;
import com.stickycoding.Rokon.RokonAudio;
import com.stickycoding.Rokon.SoundFile;

/** Sound Engine */
public class SoundEngine extends Engine {

	private RokonAudio mAudio;
	private SoundFile mStartRound;
	private SoundFile mExplosion;
	private SoundFile mBomberKilled;
	private SoundFile mBombDropped;

	private boolean soundEnable;

	public SoundEngine(BomberSlashActivity rokonActivity) {
		super(rokonActivity);
	}

	@Override
	public void createEngine() {
	}

	@Override
	public void loadEngine() {
		// Get user preferences
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mRokonActivity);
		soundEnable = sp.getBoolean("enableSound", true);

		if (soundEnable) {
			// Create Rokon audio
			mAudio = new RokonAudio();
			// Load game sounds
			mStartRound = mAudio.createSoundFile("audio/item.ogg");
			mExplosion = mAudio.createSoundFile("audio/explosion.ogg");
			mBomberKilled = mAudio.createSoundFile("audio/bomberKilled.ogg");
			mBombDropped = mAudio.createSoundFile("audio/bombDropped.ogg");
		}
	}

	@Override
	public void step() {
	}

	@Override
	public void processEvent(Event event) {
		AudioStream audioStream = null;
		if (soundEnable) {
			switch (event.type) {
			case Event.START_ROUND_EVENT:
				audioStream = mStartRound.play();
				audioStream.setRate(1);
				break;
			case Event.EXPLOSE_BOMB_EVENT:
				audioStream = mExplosion.play();
				audioStream.setRate(1);
				break;
			case Event.KILL_BOMBER_EVENT:
				audioStream = mBomberKilled.play();
				audioStream.setRate(1);
				break;
			case Event.DROP_BOMB_EVENT:
				audioStream = mBombDropped.play();
				audioStream.setRate(1);
				break;
			}
		}
	}

}
